package dgo.gadgets;
import starruby.Texture;

public class BaloonWindow {
  public int x, y;
  private int _arrowX, _arrowDirX, _arrowDirY;
  private Window _window;
  private Texture _texture;
  private String _text;
  public BaloonWindow(int targetX, int targetY, int width, int height, Texture texture, int gridSize){
    _arrowDirX = - 1; 
    _arrowDirY = - 1;
    _texture = texture;
    _window = new Window(0, 0, Math.max(width, 48), height, _texture, gridSize);

  }
  public int getHeight(){
    return _window.getFrameTexture().getHeight();
  }
  
  public int getWidth(){
    return _window.getFrameTexture().getWidth();
  }
  
  public Texture getFrameTexture(){
    return _window.getFrameTexture();
  }
    
  public void update(int targetX, int targetY, int showWidth, int showHeight){
    
    int width = getWidth();
    int height = getHeight();
    int tx = targetX - width / 2;
    int t = tx;
    int ty = targetY - height + 4;
    
    if(targetX >= 0){
      if(tx < 0){
        tx = 0;
      }
    }else{
      tx = targetX;
    }
    
    if(targetX <= showWidth){
      if(tx + width > showWidth){
        tx = showWidth - width;
      }
    }else{
      tx = targetX;
    }
    
    _arrowX = t - tx + width/2 + _window.gridSize;
    if(_arrowX < 0){
      _arrowX = 0;
    }
    if(_arrowX > width - _window.gridSize){
      _arrowX = width - _window.gridSize;
    }
    /*
        case @options[:x_fixed]
        when :left
          @arrow_dir_x = -1
        when :right
          @arrow_dir_x = 1*/
    if(targetX > showWidth / 2){
      _arrowDirX = 1;
    }else{
      _arrowDirX = -1;
    }
    
    _arrowDirY = 1;
    ty -= 6;
    
    if(ty + height > showHeight){
      _arrowDirY = 1;
    }
    if(ty < 0){
      ty = targetY + 16;
      _arrowDirY = -1;
    }
    
    this.x = tx;
    this.y = ty;
  }

  public void render(Texture s, int dx, int dy){
    _window.render(s, dx + this.x, dy + this.y);
    if(_arrowDirY == 1){
      s.renderTexture(_texture, dx + this.x + _arrowX - _window.gridSize * (_arrowDirX) - 8, 
        dy + this.y + _window.height - 2, 
        (2 + (1 + _arrowDirX)/2) * _window.gridSize, _window.gridSize, _window.gridSize, _window.gridSize);
    }
    if(_arrowDirY == -1){
      s.renderTexture(_texture, dx + this.x + _arrowX - _window.gridSize * (_arrowDirX) - 8, 
        dy + this.y - _window.gridSize + 2, 
        (2 + (1 + _arrowDirX)/2) * _window.gridSize, 0, _window.gridSize, _window.gridSize);
    }
    
    //s.renderText(_text, dx + this.x, dy + this.y);
  }
}
